﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using BridgeShips;

public class DataAdjust : MonoBehaviour {

    [SerializeField]
    [Range(-15.0f, 15.0f)]
    private float m_AddRiverAngle = 0.0f;

    [SerializeField]
    [Range(-180.0f, 180.0f)]
    private float m_AddShipLocalAngle = 0.0f;

    private bool m_isShow = false;
    

    public float RiverAngle
    {
        get { return m_AddRiverAngle; }
    }

    public float LineShipLocalAngle
    {
        get { return line_direction.value; }
    }

    public float PrefabShipLocalAngle
    {
        get { return prefab_direction.value; }
    }

    //UI组件
    private Slider river;
    private Slider line_direction;   //带式
    private Slider prefab_direction;//分制式
    //private Text txtRiver;
    //private Text txtDir;
    private Toggle m_ShowName;
    private Toggle m_bOpenAuxillaryConnection
;
    private InputField m_riverInputField;
    private InputField m_lineDirInputField;
    private InputField m_prefabDirInputField;

    private Text m_StartPosX;
    private Text m_StartPosY;
    private Text m_NexPointPosX;
    private Text m_NexPointPosY;

    private ReadXML m_readXML;
    private string url;
    void Start () {
        m_readXML = new ReadXML();
        url = Application.dataPath + "/Resources/Configs/GameDate.xml";
        //if (!m_readXML.CheckExistFiles(url))
        //{
            
        //}
        m_readXML.XmlCreate(url);

        river = GameObject.Find( "IntMsgsPanel/Slider_river" ).GetComponent<Slider>();
        line_direction = GameObject.Find( "IntMsgsPanel/Slider_dir" ).GetComponent<Slider>();
        prefab_direction = GameObject.Find("IntMsgsPanel/Slider_prefab").GetComponent<Slider>();
        //txtRiver = GameObject.Find( "IntMsgsPanel/Text_river" ).GetComponent<Text>();
        //txtDir = GameObject.Find( "IntMsgsPanel/Text_dir" ).GetComponent<Text>();
        m_ShowName = GameObject.Find("IntMsgsPanel/Toggle").GetComponent<Toggle>();
        m_bOpenAuxillaryConnection = GameObject.Find("IntMsgsPanel/OpenToggle").GetComponent<Toggle>();
        m_bOpenAuxillaryConnection.isOn = GameEntry.Instance.FsmManager.GetOpenAuxillaryConnection();
        m_riverInputField = GameObject.Find("IntMsgsPanel/river_InputField").GetComponent<InputField>();
        m_lineDirInputField = GameObject.Find("IntMsgsPanel/dir_InputField").GetComponent<InputField>();
        m_prefabDirInputField = GameObject.Find("IntMsgsPanel/prefab_InputField").GetComponent<InputField>();

        m_StartPosX = GameObject.Find("IntMsgsPanel/startPosX/Text").GetComponent<Text>();
        m_StartPosY = GameObject.Find("IntMsgsPanel/startPosY/Text").GetComponent<Text>();
        m_NexPointPosX = GameObject.Find("IntMsgsPanel/nextPointPosX/Text").GetComponent<Text>();
        m_NexPointPosY = GameObject.Find("IntMsgsPanel/nextPointPosY/Text").GetComponent<Text>();

        m_ShowName.onValueChanged.AddListener(OnToggleClick);
        m_bOpenAuxillaryConnection.onValueChanged.AddListener(OnAuxillaryConnectionToggleClick);
        m_riverInputField.onEndEdit.AddListener(OnRiverInputFiledChanged);
        m_lineDirInputField.onEndEdit.AddListener(OnLineDirInputFiledChanged);
        m_prefabDirInputField.onEndEdit.AddListener(OnPrefabDirInputFiledChanged);
        float river_value = float.Parse(m_readXML.XmlRead("river", url));
        float line_direction_value = float.Parse(m_readXML.XmlRead("line_direction", url));
        float prefab_direction_value = float.Parse(m_readXML.XmlRead("Prefab_direction", url));

        float startPosX_value = float.Parse(m_readXML.XmlRead("startLat", url));
        float startPosY_value = float.Parse(m_readXML.XmlRead("startLng", url));
        float nexPointPosX_value = float.Parse(m_readXML.XmlRead("EndLat", url));
        float nexPointPosY_value = float.Parse(m_readXML.XmlRead("EndLng", url));

        river.value = river_value;
        line_direction.value = line_direction_value;
        prefab_direction.value = prefab_direction_value;
        //txtRiver.text = "0";
        //txtDir.text = "0";
        m_riverInputField.text = river.value.ToString();
        m_lineDirInputField.text = line_direction.value.ToString();
        m_prefabDirInputField.text = prefab_direction.value.ToString();

        m_StartPosX.text = startPosX_value.ToString();
        m_StartPosY.text = startPosY_value.ToString();
        m_NexPointPosX.text = nexPointPosX_value.ToString();
        m_NexPointPosY.text = nexPointPosY_value.ToString();
}
	
	// Update is called once per frame
	void Update () {
        if (Input.GetKeyDown(KeyCode.N))
        {
            m_isShow = !m_isShow;
            //ShowName(m_isShow);
            m_ShowName.isOn = m_isShow;
        }
    }
    public void Alter() {
        m_AddRiverAngle = river.value;
        m_AddShipLocalAngle = line_direction.value;
        //txtRiver.text = river.value.ToString();
        //txtDir.text = direction.value.ToString();
        m_riverInputField.text = river.value.ToString();
        m_lineDirInputField.text = line_direction.value.ToString();
        m_prefabDirInputField.text = prefab_direction.value.ToString();
        m_readXML.XmlUpdate(url, "river", river.value.ToString());
        m_readXML.XmlUpdate(url, "line_direction", line_direction.value.ToString());
        m_readXML.XmlUpdate(url, "Prefab_direction",prefab_direction.value.ToString());
    }
    private void OnToggleClick(bool isOn) {
        ShowName(isOn);
    }
    private void OnAuxillaryConnectionToggleClick(bool isOn)
    {
        GameEntry.Instance.FsmManager.SetOpenAuxillaryConnection(isOn);
    }
    
    private void ShowName(bool isShow) {
        if (isShow)
        {
            foreach (var item in ShipsDictionary.Instance.m_billBoardObject)
            {
                item.gameObject.SetActive(true);
            }
        }
        else
        {
            foreach (var item in ShipsDictionary.Instance.m_billBoardObject)
            {
                item.gameObject.SetActive(false);
            }
        }
    }

    private void OnRiverInputFiledChanged(string value) {
        float num = float.Parse(value);
        if (num>30f)
        {
            num = 30f;
        }
        if (num<-30f)
        {
            num = -30f;
        }
        river.value = num;
        m_riverInputField.text = river.value.ToString();
        m_readXML.XmlUpdate(url, "river", river.value.ToString());  
    }
    private void OnLineDirInputFiledChanged(string value) {
        float num = float.Parse(value);
        if (num > 180)
        {
            num = 180;
        }
        if (num < -180f)
        {
            num = -180f;
        }
        line_direction.value = num;
        m_lineDirInputField.text = line_direction.value.ToString();
        m_readXML.XmlUpdate(url, "line_direction", line_direction.value.ToString());
    }
    private void OnPrefabDirInputFiledChanged(string value)
    {
        float num = float.Parse(value);
        if (num > 180)
        {
            num = 180;
        }
        if (num < -180f)
        {
            num = -180f;
        }
        prefab_direction.value = num;
        m_prefabDirInputField.text = prefab_direction.value.ToString();
        m_readXML.XmlUpdate(url, "Prefab_direction", prefab_direction.value.ToString());
    }
    
}
